package app

import (
	"APT/api/app/basics"
	"APT/internal/dao"
	"APT/internal/library/contexts"
	"APT/internal/model"
	"APT/internal/model/entity"
	"APT/internal/service"
	"APT/utility/convert"
	"context"
	"github.com/gogf/gf/v2/container/gvar"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/os/gtime"
	"time"

	"github.com/gogf/gf/v2/errors/gerror"
)

func (c *ControllerBasics) ReferrerInfo(ctx context.Context, req *basics.ReferrerInfoReq) (res *basics.ReferrerInfoRes, err error) {
	var (
		MemberInfo      *model.MemberIdentity
		YYConfig        *model.YYConfig
		Referrer        int
		LastReferrer    int
		InviteCount     int
		TodayInvite     int
		YesterdayInvite int
		WeekInvite      int
	)
	MemberInfo = contexts.GetMemberUser(ctx)
	// 获取配置
	if YYConfig, err = service.BasicsConfig().GetYYConfig(ctx); err != nil {
		return
	}
	if g.IsEmpty(YYConfig) {
		err = gerror.New("系统异常")
		return
	}
	if YYConfig.RecommendModel == "FIRST" {
		// 查询
		Referrer = MemberInfo.Id
	} else {
		LastReferrer = MemberInfo.Id
	}

	// 查询会员推荐总数量
	res = new(basics.ReferrerInfoRes)
	if InviteCount, err = dao.PmsMember.Ctx(ctx).Where(&entity.PmsMember{
		Referrer:     Referrer,
		LastReferrer: LastReferrer,
	}).OmitEmptyWhere().Count(); err != nil {
		return
	}

	res.InviteCount = gvar.New(InviteCount).Int()

	// 今日邀请

	if TodayInvite, err = dao.PmsReferrerLog.Ctx(ctx).
		Where("DATE_FORMAT(created_at,'%Y-%m-%d') = ?", gtime.Now().Format("Y-m-d")).
		Where(&entity.PmsReferrerLog{Referrer: Referrer, LastReferrer: LastReferrer}).OmitEmptyWhere().Count(); err != nil {
		return
	}
	res.TodayInvite = gvar.New(TodayInvite).Int()

	// 昨日邀请

	if YesterdayInvite, err = dao.PmsReferrerLog.Ctx(ctx).
		Where("DATE_FORMAT(created_at,'%Y-%m-%d') = ?", gtime.Now().Add(-1*24*time.Hour).Format("Y-m-d")).
		Where(&entity.PmsReferrerLog{Referrer: Referrer, LastReferrer: LastReferrer}).OmitEmptyWhere().Count(); err != nil {
		return
	}
	res.TodayInvite = gvar.New(YesterdayInvite).Int()

	// 本周邀请

	if WeekInvite, err = dao.PmsReferrerLog.Ctx(ctx).
		WhereBetween("created_at", gtime.New(convert.WeekStartDate()), gtime.Now()).
		Where(&entity.PmsReferrerLog{Referrer: Referrer, LastReferrer: LastReferrer}).OmitEmptyWhere().Count(); err != nil {
		return
	}
	res.TodayInvite = gvar.New(WeekInvite).Int()

	// 查询邀请列表
	if err = dao.PmsReferrerLog.Ctx(ctx).
		Where(&entity.PmsReferrerLog{Referrer: Referrer, LastReferrer: LastReferrer}).WithAll().
		OmitEmptyWhere().Page(req.Page, req.PerPage).ScanAndCount(&res.List, &res.Count, false); err != nil {
		return
	}

	return
}

func (c *ControllerBasics) ReferrerList(ctx context.Context, req *basics.ReferrerListReq) (res *basics.ReferrerListRes, err error) {
	var (
		MemberInfo   *model.MemberIdentity
		YYConfig     *model.YYConfig
		Referrer     int
		LastReferrer int
	)
	MemberInfo = contexts.GetMemberUser(ctx)
	// 获取配置
	if YYConfig, err = service.BasicsConfig().GetYYConfig(ctx); err != nil {
		return
	}
	if g.IsEmpty(YYConfig) {
		err = gerror.New("系统异常")
		return
	}
	if YYConfig.RecommendModel == "FIRST" {
		// 查询
		Referrer = MemberInfo.Id
	} else {
		LastReferrer = MemberInfo.Id
	}

	// 查询会员推荐总数量
	res = new(basics.ReferrerListRes)
	// 查询邀请列表
	if err = dao.PmsReferrerLog.Ctx(ctx).
		Where(&entity.PmsReferrerLog{Referrer: Referrer, LastReferrer: LastReferrer}).WithAll().
		OmitEmptyWhere().Page(req.Page, req.PerPage).ScanAndCount(&res.List, &res.Count, false); err != nil {
		return
	}

	return
}
